<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            库存现存量-打码
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">仓库</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描 仓库" id="workCenterCode" value="" onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCenter()"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">库位</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="选择/扫描 库位" id="workCellCode" value="" onclick="getIdSelector();" />
                    </div>
                    <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectWorkCell()"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">蓝牙设备</div>
                    <select id="bluetooth">
                    </select>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="doSearch()">查询</div>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="bluetoothConnect()">蓝牙连接</div>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info" style="width:30%;margin-left:2%;" onclick="wmstockPrintByNum()">录数打印</div>
        <div class="aui-btn aui-btn-info" style="width:30%;margin-left:2%;" onclick="wmstockPrintByPacknum()">录包装数打印</div>
        <div class="aui-btn aui-btn-info" style="width:30%;margin-left:2%;" onclick="wmstockPrint()">批量打印</div>
        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;">
                        <td algin='center'></td>
                        <!-- <td algin='center'>序号</td> -->
                        <td algin='center'>物料号</td>
                        <td algin='center'>批次号</td>
                        <td algin='center'>现有数</td>
                        <!-- <td algin='center'>冻结数</td> -->
                        <td algin='center'>仓库编码</td>
                        <td algin='center'>库位编码</td>
                        <td style="display:none;">物料名称</td>
                        <td style="display:none;">规格型号</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div>
    </div>

    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var workCenterGid = ""; //仓库GID
    var workCenterCode = ""; //仓库编码
    var workCenterName = ""; //仓库名称
    var workCellGid = ""; //库位GID
    var workCellCode = ""; //库位编码
    var workCellName = ""; //库位名称

    //初始化
    apiready = function() {
        $("#workCenterCode").focus();
        idSelector = "workCenterCode";

        //获取蓝牙列表
        getBluetoothCombox();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //查询蓝牙设备
    function getBluetoothCombox() {
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.searchBluetooth({}, function(ret, err) {
            if (ret) {
                if (ret.code == "S") {
                    var list = ret.list;
                    var html = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                    }
                    $api.html($api.byId("bluetooth"), html);

                    //取缓存中蓝牙设备打印机
                    var bluetooth = localStorage.getItem('bluetooth');
                    if (bluetooth != null && bluetooth != undefined && bluetooth != "") {
                        $("#bluetooth").val(bluetooth);
                    }

                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //蓝牙连接
    function bluetoothConnect() {
        var address = $("#bluetooth").val();
        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙"
            });
            return;
        }
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.connectBluetooth({
            address: address,
        }, function(ret, err) {
            if (ret) {
                // api.hideProgress();
                if (ret.code == "S") {
                    api.toast({
                        msg: ret.message
                    });
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                // api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }


    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {
            //alert(ret);扫码的结果都是编码
            //1：仓库
            if (idSelector == 'workCenterCode') {

                $("#workCenterCode").val(ret);
                //查询仓库
                getWorkCenter(ret, idSelector);

                //2:库位
            } else if (idSelector == 'workCellCode') {

                $("#workCellCode").val(ret);
                //查询库位
                getWorkCell(ret, idSelector);

            }
        });
    }

    //1：仓库扫描校验
    function getWorkCenter(code, inputId) {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem('un');
        var url = serverUrl + "/app/getWorkCenter";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                workCenterCode: code,
                mrlCode: "",
                lotCode: "",
                qty: "",
                un:un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    workCenterGid = list[0].workCenterGid;
                    workCenterCode = list[0].workCenterCode;
                    workCenterName = list[0].workCenterName;
                    document.getElementById(inputId).value = list[0].workCenterCode + ":" + list[0].workCenterName;
                    $("#workCellCode").focus();
                    idSelector = "workCellCode";

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#workCenterCode").focus();
                    $("#workCenterCode").select();
                }
            }
        });
    }

    //2：选中仓库
    function goSelectWorkCenter() {
        api.openWin({
            name: 'uwmSelectWorkCenter',
            url: 'uwmSelectWorkCenter.html',
            pageParam: {
                flag: "in",
                name: "uwmStock_print",
                mrlCode: "",
                lotCode: "",
                qty: ""
            }
        });
    }

    //仓库选择后回调函数
    function setWorkCenter(id, code, name) {
        workCenterGid = id;
        workCenterCode = code;
        workCenterName = name;
        //$("#workCenterCode").attr("value",code+":"+name);
        document.getElementById('workCenterCode').value = code + ":" + name;
        $("#workCellCode").focus();
        idSelector = "workCellCode";
    }

    //3；PDA自带扫描回车事件
    $("#workCenterCode").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //仓库编码
            $("#workCenterCode").val(sm);
            // console.log("扫描回车事件：" + sm);
            //校验改箱码是否存在
            getWorkCenter(sm, "workCenterCode");
        }
    });

    //5:库位扫描校验
    function getWorkCell(code, inputId) {
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCell";
        var data = {
            values: {
                flag: "in_rk", //库内库间标识（out_kn库内  out_kj库间,in_rk入库出库）
                workCenterGid: workCenterGid, //仓库GID
                workCellCode: code, //库位编码
                relWorkCellGid: "", //调入库位
                tm: "",
                workCellFlag: "", //原库位调入库位flag
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    workCellGid = list[0].workCellGid;
                    workCellCode = list[0].workCellCode;
                    workCellName = list[0].workCellName;
                    document.getElementById(inputId).value = list[0].workCellCode + ":" + list[0].workCellName;

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#workCellCode").focus();
                    $("#workCellCode").select();
                }
            }
        });
    }

    //6：选择库位
    function goSelectWorkCell() {
        api.openWin({
            name: 'uwmSelectWorkCell',
            url: 'uwmSelectWorkCell.html',
            pageParam: {
                flag: "in_rk",
                name: "uwmStock_print",
                tm: "",
                workCenterGid: workCenterGid,
                relWorkCellGid: "",
                workCellFlag: ""
            }
        });
    }

    //库位选择后回调函数
    function setWorkCell(id, code, name) {
        workCellGid = id;
        workCellCode = code;
        workCellName = name;
        document.getElementById('workCellCode').value = code + ":" + name;
    }

    //7；PDA自带扫描回车事件-库位
    $("#workCellCode").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //仓库编码
            $("#workCellCode").val(sm);
            //校验改箱码是否存在
            getWorkCell(sm, "workCellCode");
        }
    });


    //查询
    function doSearch() {

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/doSearchStock";
        var data = {
            values: {
                workCenterGid: workCenterGid,
                workCellGid: workCellGid,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {
                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>";
                        html += "<td style='width:40px;'><input name='sub' type='checkbox' /></td>";
                        // html += "<td name='seqno'>" + (i + 1) + "</td>";
                        html += "<td name='mrlCode'>" + list[i].mrlCode + "</td>";
                        html += "<td name='lotCode'>" + list[i].lotCode + "</td>";
                        html += "<td name='qty'>" + list[i].qty + "</td>";
                        // html += "<td name='frezzQty'>" + list[i].frezzQty + "</td>";
                        html += "<td name='workCenterCode'>" + list[i].workCenterCode + "</td>";
                        html += "<td name='workCellCode'>" + list[i].workCellCode + "</td>";
                        html += "<td name='mrlName' style='display:none;'>" + list[i].mrlName + "</td>";
                        html += "<td name='specification' style='display:none;'>" + list[i].specification + "</td>";
                        html += "</tr>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);

                    //失败
                } else if (myobj.code == "1") {

                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //录数打印
    function wmstockPrintByNum() {

        var address = $("#bluetooth").val();

        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        check.each(function() {
            count++;
        });

        //选中行不为1
        if (count == 0) {
            api.toast({
                msg: '请选择至少一条数据！',
                duration: 3000
            });
            return;
        }

        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: '请选择打印机!',
                duration: 3000
            });
            return;
        }

        localStorage.setItem('bluetooth', address);

        var dialogBox = api.require('dialogBox');
        dialogBox.input({
            keyboardType: 'default',
            texts: {
                title: '数量',
                placeholder: '数量',
                leftBtnTitle: '取消',
                rightBtnTitle: '确定'
            },
            styles: {
                bg: '#fff',
                corner: 2,
                w: 300,
                h: 200,
                title: {
                    h: 40,
                    alignment: 'center',
                    size: 14,
                    color: '#000',
                    marginT: 30,
                },
                input: {
                    h: 60,
                    y: 30,
                    marginT: 15,
                    marginLeft: 10,
                    marginRight: 10,
                    textSize: 14,
                    textColor: '#000',
                    corner: 5, //（可选项）数字类型；对输入框圆角大小；默认：0

                },
                dividingLine: {
                    width: 0.5,
                    color: '#696969'
                },
                left: {
                    bg: 'rgba(0,0,0,0)',
                    color: '#007FFF',
                    size: 12,
                    h: 40, //(可选项) 数字类型；右边按钮的高度；默认：40
                    w: 60, //(可选项) 数字类型；左边按钮的宽度；默认：提示框的宽度的一半
                    marginB: 10, //(可选项) 数字类型；左边按离底部的边距；默认：0
                    corner: 5, //(可选项) 数字类型；左边按钮的圆角；默认：0
                    widhtBorder: 1, //(可选项) 数字类型；左边按钮的边宽；默认：0
                    normalBorderColor: '#778899', //(可选项) 数字类型；右边按钮的边宽颜色；默认：'rgba(0,0,0,0)'
                    activeBorderColor: '#0000CD', //(可选项) 数字类型；右边按钮的高亮状态下边宽颜色；默认：'rgba(0,0,0,0)'
                    activeColor: '#006400', //(可选项) 数字类型；右边按钮的高亮状态下标题颜色；默认：'rgba(0,0,0,0)'
                    activeBg: '#6B8E23' //(可选项) 数字类型；右边按钮的高亮状态下背景颜色；默认：'rgba(0,0,0,0)'
                },
                right: {
                    bg: 'rgba(0,0,0,0)',
                    color: '#007FFF',
                    size: 12,
                    h: 40, //(可选项) 数字类型；右边按钮的高度；默认：40
                    w: 60, //(可选项) 数字类型；右边按钮的宽度；默认：提示框的宽度的一半
                    marginB: 10, //(可选项) 数字类型；右边按离底部的边距；默认：0
                    corner: 5, //(可选项) 数字类型；右边按钮的圆角；默认：0
                    widhtBorder: 1, //(可选项) 数字类型；右边按钮的边宽；默认：0
                    normalBorderColor: '#778899', //(可选项) 数字类型；右边按钮的边宽颜色；默认：'rgba(0,0,0,0)'
                    activeBorderColor: '#0000CD', //(可选项) 数字类型；右边按钮的高亮状态下边宽颜色；默认：'rgba(0,0,0,0)'
                    activeColor: '#006400', //(可选项) 数字类型；右边按钮的高亮状态下标题颜色；默认：'rgba(0,0,0,0)'
                    activeBg: '#6B8E23' //(可选项) 数字类型；右边按钮的高亮状态下背景颜色；默认：'rgba(0,0,0,0)'
                }
            }
        }, function(ret) {
            if (ret.eventType == 'left') {
                closeDialog();
            } else {
                var newQty = ret.text;
                if (newQty == null || newQty == "" || newQty == undefined || newQty <= 0) {
                    api.toast({
                        msg: '数量必须大于0!',
                        duration: 3000
                    });
                    return;
                }
                closeDialog();

                var list = [];

                var mrlCode = "";
                var mrlName = "";
                var lotCode = "";
                var specification = "";
                check.each(function() { //遍历
                    var row = $(this).parent("td").parent("tr");
                    mrlCode = row.find("[name='mrlCode']").html();
                    mrlName = row.find("[name='mrlName']").html();
                    lotCode = row.find("[name='lotCode']").html();
                    specification = row.find("[name='specification']").html();

                    list.push({
                        "mrlCode": mrlCode,
                        "mrlName": mrlName,
                        "lotCode": lotCode,
                        "qty": newQty,
                        "specification": specification
                    });
                });

                printByNum(address, list);
            }
        });

    }

    function printByNum(address, list) {
        if (workCenterGid == "" || workCenterGid == null || workCenterGid == undefined) {
          api.toast({
              msg: '请选择/扫描 仓库',
              duration: 3000
          });
          return;
      }
      if (workCellGid == "" || workCellGid == null || workCellGid == undefined) {
          api.toast({
              msg: '请选择/扫描 库位',
              duration: 3000
          });
          return;
      }
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wmstockPrintByNum";
        var data = {
            values: {
                workCenterGid: workCenterGid,
                workCellGid: workCellGid,
                list: JSON.stringify(list),
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {

                    var json = myobj.json;
                    //调用PDA打印
                    printPDA(json, address);

                } else if (myobj.code == "1") {

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //录包装数打印
    function wmstockPrintByPacknum() {
        var address = $("#bluetooth").val();

        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        check.each(function() {
            count++;
        });

        //选中行不为1
        if (count == 0) {
            api.toast({
                msg: '请选择至少一条数据！',
                duration: 3000
            });
            return;
        }

        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: '请选择打印机!',
                duration: 3000
            });
            return;
        }

        localStorage.setItem('bluetooth', address);

        var dialogBox = api.require('dialogBox');
        dialogBox.input({
            keyboardType: 'default',
            texts: {
                title: '数量',
                placeholder: '数量',
                leftBtnTitle: '取消',
                rightBtnTitle: '确定'
            },
            styles: {
                bg: '#fff',
                corner: 2,
                w: 300,
                h: 200,
                title: {
                    h: 40,
                    alignment: 'center',
                    size: 14,
                    color: '#000',
                    marginT: 30,
                },
                input: {
                    h: 60,
                    y: 30,
                    marginT: 15,
                    marginLeft: 10,
                    marginRight: 10,
                    textSize: 14,
                    textColor: '#000',
                    corner: 5, //（可选项）数字类型；对输入框圆角大小；默认：0

                },
                dividingLine: {
                    width: 0.5,
                    color: '#696969'
                },
                left: {
                    bg: 'rgba(0,0,0,0)',
                    color: '#007FFF',
                    size: 12,
                    h: 40, //(可选项) 数字类型；右边按钮的高度；默认：40
                    w: 60, //(可选项) 数字类型；左边按钮的宽度；默认：提示框的宽度的一半
                    marginB: 10, //(可选项) 数字类型；左边按离底部的边距；默认：0
                    corner: 5, //(可选项) 数字类型；左边按钮的圆角；默认：0
                    widhtBorder: 1, //(可选项) 数字类型；左边按钮的边宽；默认：0
                    normalBorderColor: '#778899', //(可选项) 数字类型；右边按钮的边宽颜色；默认：'rgba(0,0,0,0)'
                    activeBorderColor: '#0000CD', //(可选项) 数字类型；右边按钮的高亮状态下边宽颜色；默认：'rgba(0,0,0,0)'
                    activeColor: '#006400', //(可选项) 数字类型；右边按钮的高亮状态下标题颜色；默认：'rgba(0,0,0,0)'
                    activeBg: '#6B8E23' //(可选项) 数字类型；右边按钮的高亮状态下背景颜色；默认：'rgba(0,0,0,0)'
                },
                right: {
                    bg: 'rgba(0,0,0,0)',
                    color: '#007FFF',
                    size: 12,
                    h: 40, //(可选项) 数字类型；右边按钮的高度；默认：40
                    w: 60, //(可选项) 数字类型；右边按钮的宽度；默认：提示框的宽度的一半
                    marginB: 10, //(可选项) 数字类型；右边按离底部的边距；默认：0
                    corner: 5, //(可选项) 数字类型；右边按钮的圆角；默认：0
                    widhtBorder: 1, //(可选项) 数字类型；右边按钮的边宽；默认：0
                    normalBorderColor: '#778899', //(可选项) 数字类型；右边按钮的边宽颜色；默认：'rgba(0,0,0,0)'
                    activeBorderColor: '#0000CD', //(可选项) 数字类型；右边按钮的高亮状态下边宽颜色；默认：'rgba(0,0,0,0)'
                    activeColor: '#006400', //(可选项) 数字类型；右边按钮的高亮状态下标题颜色；默认：'rgba(0,0,0,0)'
                    activeBg: '#6B8E23' //(可选项) 数字类型；右边按钮的高亮状态下背景颜色；默认：'rgba(0,0,0,0)'
                }
            }
        }, function(ret) {
            //取消
            if (ret.eventType == 'left') {
                closeDialog();

                //确定按钮
            } else {
                var packQty = ret.text;
                if (packQty == null || packQty == "" || packQty == undefined || packQty <= 0) {
                    api.toast({
                        msg: '包装数量必须大于0!',
                        duration: 3000
                    });
                    return;
                }
                closeDialog();

                var list = [];

                var mrlCode = "";
                var mrlName = "";
                var lotCode = "";
                var qty = "";
                var specification = "";
                check.each(function() { //遍历
                    var row = $(this).parent("td").parent("tr");
                    mrlCode = row.find("[name='mrlCode']").html();
                    mrlName = row.find("[name='mrlName']").html();
                    qty = row.find("[name='qty']").html();
                    lotCode = row.find("[name='lotCode']").html();
                    specification = row.find("[name='specification']").html();

                    list.push({
                        "mrlCode": mrlCode,
                        "mrlName": mrlName,
                        "lotCode": lotCode,
                        "qty": qty,
                        "packQty": packQty,
                        "specification": specification
                    });
                });

                printByPackNum(address, list);
            }
        });
    }

    function printByPackNum(address, list) {
        if (workCenterGid == "" || workCenterGid == null || workCenterGid == undefined) {
          api.toast({
              msg: '请选择/扫描 仓库',
              duration: 3000
          });
          return;
      }
      if (workCellGid == "" || workCellGid == null || workCellGid == undefined) {
          api.toast({
              msg: '请选择/扫描 库位',
              duration: 3000
          });
          return;
      }
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wmstockPrintByPackNum";
        var data = {
            values: {
                workCenterGid :workCenterGid,
                workCellGid :workCellGid,
                list: JSON.stringify(list),workCellGid,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {

                    var json = myobj.json;
                    //调用PDA打印
                    printPDA(json, address);

                } else if (myobj.code == "1") {

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //批量打印
    function wmstockPrint() {
        var address = $("#bluetooth").val();

        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        check.each(function() {
            count++;
        });

        //选中行不为1
        if (count == 0) {
            api.toast({
                msg: '请选择至少一条数据！',
                duration: 3000
            });
            return;
        }

        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: '请选择打印机!',
                duration: 3000
            });
            return;
        }

        localStorage.setItem('bluetooth', address);

        var list = [];

        var mrlCode = "";
        var mrlName = "";
        var lotCode = "";
        var qty = "";
        var specification = "";
        check.each(function() { //遍历
            var row = $(this).parent("td").parent("tr");
            mrlCode = row.find("[name='mrlCode']").html();
            mrlName = row.find("[name='mrlName']").html();
            qty = row.find("[name='qty']").html();
            lotCode = row.find("[name='lotCode']").html();
            specification = row.find("[name='specification']").html();

            list.push({
                "mrlCode": mrlCode,
                "mrlName": mrlName,
                "lotCode": lotCode,
                "qty": qty,
                "packQty": qty,
                "specification": specification
            });
        });

        printByPackNum(address, list);
    }

    //打印
    function printPDA(json, address) {
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.printerPlatformCt({
            json: json, //打印数据JSON
            address: address //蓝牙地址
        }, function(ret, err) {
            if (ret) {
                if (ret.code == "S") {
                    api.toast({
                        msg: "打印成功"
                    });
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    function closeDialog() {
        var dialogBox = api.require('dialogBox');
        dialogBox.close({
            dialogName: 'input'
        });
    }

    //订单返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
